package cn.tedu;

import java.sql.*;

public class Demo03 {
    public static void main(String[] args) throws SQLException {
        Connection conn = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/empdb?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false",
                "root","root"
        );
        System.out.println("连接对象"+conn);
        Statement s = conn.createStatement();
//        s.executeUpdate("insert into emp(name) values('Tom')");
//        s.executeUpdate("delete from emp where name='Tom'");
        ResultSet rs = s.executeQuery("select name,sal from emp");
        //取出结果集对象中的数据 遍历结果集对象
        // rs.next()让游标往下移动一格,返回值表示是否有下一条数据
        while(rs.next()){
            //通过字段名从结果集对象中取出游标指向的数据
//            String name = rs.getString("name");
//            double sal = rs.getDouble("sal");
            //通过字段位置获取数据
            String name = rs.getString(1);
            double sal = rs.getDouble(2);
            System.out.println(name+":"+sal);
        }
        conn.close();
        System.out.println("执行完成!");
    }
}
